package com.rui1024.servlet;

import com.rui1024.dao.UserDao;
import com.rui1024.dao.impl.UserDaoImpl;
import com.rui1024.pojo.Management_user;
import com.rui1024.pojo.User;
import com.rui1024.util.JDBCUtil;
import com.rui1024.util.Md5Util;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //接收参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String remember = req.getParameter("remember");
        String userTypes = req.getParameter("userType");
        String userType = new String(userTypes.getBytes("utf-8"),"utf-8");

        HttpSession session = req.getSession();

        System.out.println("账号种类： "+userType);
        System.out.println(username);
        System.out.println(password);

//        try {
//            System.out.println(Md5Util.encodePwdByMd5("123"));
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }


        User user = new User(username, password);
        Management_user management_user = new Management_user(username, password);

        //获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date date = new Date();
        String datetime = sdf.format(date);

        
        //调用dao
        UserDao userDao = new UserDaoImpl();
        UserDao management_userdao = new UserDaoImpl();
        JDBCUtil jdbcUtil = new JDBCUtil();
        Connection con = null;

        if (userType.equals("管理员")) {
            try {
                con = jdbcUtil.getCon();
                Management_user currentUser_man =  userDao.loginman(con, management_user);
                System.out.println(currentUser_man);
                //响应
                if (currentUser_man != null) {
                    //成功
                    session.setAttribute("currentUser_man", currentUser_man);
                    req.setAttribute("currentUser_man",currentUser_man);
                    req.setAttribute("datetime",datetime);
                    req.getRequestDispatcher("manageControl.jsp").forward(req, resp);
                } else {
                    //失败
                    req.setAttribute("user", user);
                    req.setAttribute("errorMsg", "用户名或密码错误！");
                    req.getRequestDispatcher("/login.jsp").forward(req, resp);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    jdbcUtil.closeCon(con);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        else if (userType.equals("普通用户")) {
            try {
                con = jdbcUtil.getCon();
                User currentUser = userDao.login(con, user);
                //响应
                if (currentUser != null) {
                    //成功
                    if ("remember-me".equals(remember)) {
                        Cookie userCookie = new Cookie("userCookie", username + "-" + password);
                        userCookie.setMaxAge(1 * 60 * 60 * 24 * 7);
                        resp.addCookie(userCookie);
                    }
                    session.setAttribute("currentUser", currentUser);
                    resp.sendRedirect("usermain.jsp");
                } else {
                    //失败
                    req.setAttribute("user", user);
                    req.setAttribute("errorMsg", "用户名或密码错误！");
                    req.getRequestDispatcher("/login.jsp").forward(req, resp);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    jdbcUtil.closeCon(con);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
